package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Dept;
import com.hopu.service.DeptService;
import com.hopu.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("part")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptServiceImpl;

    /**
     * 查询部门数据（分页查询）
     * @return
     */
    @PostMapping("findAllDept")
    private ResultUtils findAllDept(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                                    @RequestParam(value = "pflag",defaultValue = "0") int pflag){
        PageHelper.startPage(pn,14);
        List<Dept> list = deptServiceImpl.findAllDept(pflag);
        PageInfo page = new PageInfo(list,5);
        return  new ResultUtils(200,"SUCCESS",page);
    }

    /**
     * 根据id或者pname查找部门
     * @param pname
     * @return
     */
    @PostMapping("findPart")
    private ResultUtils findPart(@RequestParam(value = "pname",required = false) String pname,
                                 @RequestParam(value = "pid",required = false) Integer pid){
        System.out.println(pname+"----"+pid);
            Dept dept = deptServiceImpl.findPart(pname,pid);
            System.out.println(dept);
            if (!ObjectUtils.isEmpty(dept)) {
                return new ResultUtils(200, "SUCCESS", dept);
            }
        return new ResultUtils(400,"ERROR",null);
    }

    /**
     * 添加部门
     * @return
     */
    @PostMapping("addPart")
    private ResultUtils addPart(@RequestParam String pname,@RequestParam String premark){
//        System.out.println(pname+"===="+premark);
     int num = deptServiceImpl.addPart(pname,premark);
     return new ResultUtils(200,"SUCCESS",null);
    }

    /**
     * 撤销部门
     * @param pid
     * @return
     */
    @PostMapping("delPart")
    private ResultUtils delPart(@RequestParam int pid){
        int num = deptServiceImpl.delPart(pid);
        return new ResultUtils(200,"SUCCESS",1);
    }

    /**
     * 恢复部门
     * @param pid
     * @return
     */
    @PostMapping("huiFuPart")
    private ResultUtils huiFuPart(@RequestParam("pid") int pid){
        System.out.println(pid);
        int num = deptServiceImpl.huiFuPart(pid);
        return new ResultUtils(200,"SUCCESS",1);
    }
    /**
     * 修改部门
     * @param pid
     * @return
     */
    @PostMapping("updatePart")
    private ResultUtils updatePart(@RequestParam(value = "pid") int pid,
                                   @RequestParam(value = "pname") String pname,
                                   @RequestParam(value = "premark") String premark){
        int num = deptServiceImpl.updatePart(pid,pname,premark);
        return new ResultUtils(200,"SUCCESS",1);
    }

    /**
     * 查询是否有重名
     * @param pname
     * @param pid
     * @return
     */
    @PostMapping("findPartByCondition")
    private ResultUtils findPartByCondition(@RequestParam(value = "pname") String pname,
                                            @RequestParam(value = "pid") int pid){
        Dept dept = deptServiceImpl.findPartByCondition(pname,pid);
        if (!ObjectUtils.isEmpty(dept)){
            return new ResultUtils(200,"SUCCESS",1);
        }
        return new ResultUtils(200,"SUCCESS",0);
    }

    /**
     * 查询所有部门
     * @return
     */
    @PostMapping("findAllPart")
    private ResultUtils findAllPart(){
        List<Dept> dept = deptServiceImpl.findAllPart();
        if (!CollectionUtils.isEmpty(dept)){
            return new ResultUtils(200,"SUCCESS",dept);
        }
        return new ResultUtils(200,"SUCCESS",null);
    }


}
